package jjn.carl.stack_queue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Jjn
 * @since 2023/7/7 23:03
 */
public class MyStack {
    
    private final Queue<Integer> first;
    private final Queue<Integer> second;
    
    public MyStack() {
        this.first = new LinkedList<>();
        this.second = new LinkedList<>();
    }
    
    public void push(int x) {
        while (!first.isEmpty()) {
            second.offer(first.poll());
        }
        first.offer(x);
        while (!second.isEmpty()) {
            first.offer(second.poll());
        }
    }
    
    public int pop() {
        return first.peek() == null ? -1 : first.poll();
    }
    
    public int top() {
        return first.peek() == null ? -1 : first.peek();
    }
    
    public boolean empty() {
        return first.isEmpty();
    }
    
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        System.out.println("myStack.top() = " + myStack.top());
        System.out.println("myStack.pop() = " + myStack.pop());
        System.out.println("myStack.empty() = " + myStack.empty());
    }
}
